// API configuration utility
const API_CONFIG = {
  // Get the base URL from environment variables
  baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
  streamURL: import.meta.env.VITE_VIDEO_BASE_URL || '/api/videos',
  // Define all API endpoints
  endpoints: {
    videos: '/videos',
    labels: '/labels',
    images: '/images',
    login: '/login',
    register: '/register',
    health: '/health',
    configs: '/configs',
    cameras: '/cameras',
    users: '/users'
  },

  // Helper method to build full API URLs
  getURL(endpoint) {
    return `${this.baseURL}${this.endpoints[endpoint]}`;
  },

  // Helper method for fetch requests with common configuration
  async fetch(endpoint, options = {}) {
    const url = this.getURL(endpoint);
    console.log(url);
    const defaultOptions = {
      headers: {
        'Content-Type': 'application/json',
        ...options.headers
      },
      credentials: 'include',
      ...options
    };
    
    try {
      const response = await window.fetch(url, defaultOptions);
      
      // 返回完整的 Response 对象而不是直接解析 JSON
      return response;
    } catch (error) {
      console.error(`Error fetching ${url}:`, error);
      throw error;
    }
  },

  // Helper method to fetch and parse JSON response
    async fetchJSON(endpoint, options = {}) {
      const response = await this.fetch(endpoint, options);
      
      if (!response.ok) {
        throw new Error(`API request failed: ${response.status} ${response.statusText}`);
      }
      
      if (response.status === 204) {
        return null; // No content
      }
      
      return await response.json();
    }
};

export default API_CONFIG;